package com.xzuf.datatransfer;

import java.io.Serializable;
import java.util.StringTokenizer;
import java.util.Vector;

public class SelSqlVO implements Serializable {

	public String m_strSelectSub;
	public String m_strFromSub;
	public String m_strWhereSub;

	public SelSqlVO() {
		m_strSelectSub = null;
		m_strFromSub = null;
		m_strWhereSub = null;
	}

	public Object clone() {
		Object o = null;
		try {
			o = super.clone();
		} catch (Exception exception) {
		}
		SelSqlVO vo = (SelSqlVO) o;
		vo.setFromSub(getFromSub());
		vo.setSelectSub(getSelectSub());
		vo.setWhereSub(getWhereSub());
		return vo;
	}

	public int getColNum() {
		StringTokenizer st = new StringTokenizer(getSelectSub(), ",");
		int count = 0;
		while (st.hasMoreTokens()) {
			String s = st.nextToken().trim();
			if (s != null && s.length() > 0)
				count++;
		}
		return count;
	}

	public String[] getCols() {
		StringTokenizer st = new StringTokenizer(getSelectSub(), ",");
		Vector v = new Vector();
		while (st.hasMoreTokens()) {
			String s = st.nextToken().trim();
			if (s != null && s.length() > 0)
				v.add(s);
		}
		String cols[] = new String[v.size()];
		if (v.size() > 0)
			v.copyInto(cols);
		return cols;
	}

	public String getEntityName() {
		return null;
	}

	public String getFromSub() {
		return m_strFromSub;
	}

	public String getSelectSql() {
		if (m_strFromSub != null && m_strFromSub.length() > 0 && m_strSelectSub != null && m_strSelectSub.length() > 0) {
			StringBuffer sb = new StringBuffer();
			sb.append("select ").append(m_strSelectSub).append(" from ").append(m_strFromSub);
			if (m_strWhereSub != null && m_strWhereSub.length() > 0)
				sb.append(" where ").append(m_strWhereSub);
			else
				sb.append(" where 1=1 ");
			return sb.toString();
		} else {
			return null;
		}
	}

	public String getSelectSub() {
		return m_strSelectSub;
	}

	public VariableVO[] getVariables() {
		Vector v = new Vector();
		Vector v1 = new Vector();
		String prefix = "{$";
		int prefixLen = prefix.length();
		String suffix = "$}";
//		int suffixLen = suffix.length();
		String whereSub = getWhereSub();
		int len = whereSub.length();
		int iSuffix;
		for (int iPrefix = whereSub.indexOf(prefix); iPrefix != -1 && iPrefix < len; iPrefix = whereSub.indexOf(prefix, iSuffix)) {
			iSuffix = whereSub.indexOf(suffix, iPrefix);
			if (iSuffix == -1 || iSuffix >= len)
				break;
			VariableVO varVO = new VariableVO();
			varVO.setType(0);
			String var = whereSub.substring(iPrefix + prefixLen, iSuffix).trim();
			varVO.setVarName(var);
			if (!v1.contains(var)) {
				v1.add(var);
				v.add(varVO);
			}
		}

		String prefixDigi = "{#";
		int prefixDigiLen = prefixDigi.length();
		String suffixDigi = "#}";
//		int suffixDigLen = suffixDigi.length();
		int iSuffixDigi;
		for (int iPrefixDigi = whereSub.indexOf(prefixDigi); iPrefixDigi != -1 && iPrefixDigi < len; iPrefixDigi = whereSub.indexOf(prefixDigi, iSuffixDigi)) {
			iSuffixDigi = whereSub.indexOf(suffixDigi, iPrefixDigi);
			if (iSuffixDigi == -1 || iSuffixDigi >= len)
				break;
			VariableVO varVO = new VariableVO();
			varVO.setType(1);
			String var = whereSub.substring(iPrefixDigi + prefixDigiLen,iSuffixDigi).trim();
			varVO.setVarName(var);
			if (!v1.contains(var)) {
				v1.add(var);
				v.add(varVO);
			}
		}

		VariableVO varVOs[] = new VariableVO[v.size()];
		if (v.size() > 0)
			v.copyInto(varVOs);
		return varVOs;
	}

	public String getWhereSub() {
		return m_strWhereSub;
	}

	public void setFromSub(String newFromSub) {
		newFromSub.replace('\uFF0C', ',');
		m_strFromSub = newFromSub;
	}

	public void setSelectSub(String newSelectSub) {
		newSelectSub.replace('\uFF0C', ',');
		m_strSelectSub = newSelectSub;
	}

	public void setWhereSub(String newWhereSub) {
		m_strWhereSub = newWhereSub;
	}
}
